"""
IP配置模型
"""
from datetime import datetime
from app.extensions import db


class IpConfig(db.Model):
    """IP配置模型"""
    __tablename__ = 'ip_config'
    
    id = db.Column(db.Integer, primary_key=True)
    workshop = db.Column(db.String(100), nullable=False)  # 车间
    line = db.Column(db.String(100), nullable=False)  # 线别
    ip = db.Column(db.String(15), nullable=False)  # IP地址
    database_path = db.Column(db.String(255), nullable=False)  # 数据库路径
    segment_path = db.Column(db.String(255), nullable=False)  # 分档路径
    current_segment = db.Column(db.String(100), nullable=False)  # 在用分档
    is_enabled = db.Column(db.Boolean, default=True)  # 是否启用
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'workshop': self.workshop,
            'line': self.line,
            'ip': self.ip,
            'database_path': self.database_path,
            'segment_path': self.segment_path,
            'current_segment': self.current_segment,
            'is_enabled': self.is_enabled,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S')
        }